// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h

#ifndef __C_D3D9_NORMAL_MAPMATERIAL_RENDERER_H_INCLUDED__
#define __C_D3D9_NORMAL_MAPMATERIAL_RENDERER_H_INCLUDED__

#include "IrrCompileConfig.h"
#ifdef _IRR_WINDOWS_

#ifdef _IRR_COMPILE_WITH_DIRECT3D_9_
#if defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
#include "irrMath.h"    // needed by borland for sqrtf define
#endif 
#include <d3d9.h>

#include "CD3D9ShaderMaterialRenderer.h"
#include "IShaderConstantSetCallBack.h"

#include "IVideoDriver.h"
#include "SLight.h"
#include "IMaterialRendererServices.h"

namespace irr
{
namespace video
{

#ifdef DELPHI_WRAP
	class CD3D9NormalMapRendererShaderCallBack : public IShaderConstantSetCallBack
	{
	public:
		virtual void _STDCALL_ OnSetConstants(IMaterialRendererServices* services, s32 userData)
		{
			{
				video::IVideoDriver* driver = services->getVideoDriver();

				// set transposed world matrix
				services->setVertexShaderConstant(driver->getTransform(video::ETS_WORLD).getTransposed().pointer(), 0, 4);

				// set transposed worldViewProj matrix
				core::matrix4 worldViewProj(driver->getTransform(video::ETS_PROJECTION));
				worldViewProj *= driver->getTransform(video::ETS_VIEW);
				worldViewProj *= driver->getTransform(video::ETS_WORLD);
				services->setVertexShaderConstant(worldViewProj.getTransposed().pointer(), 8, 4);

				// here we've got to fetch the fixed function lights from the
				// driver and set them as constants

				u32 cnt = driver->getDynamicLightCount();

				for (u32 i=0; i<2; ++i)
				{
					SLight light; 

					if (i<cnt)
						light = driver->getDynamicLight(i);
					else
					{
						light.DiffuseColor.set(0,0,0); // make light dark
						light.Radius = 1.0f;
					}

					light.DiffuseColor.a = 1.0f/(light.Radius*light.Radius); // set attenuation

					services->setVertexShaderConstant(reinterpret_cast<const f32*>(&light.Position), 12+(i*2), 1);
					services->setVertexShaderConstant(reinterpret_cast<const f32*>(&light.DiffuseColor), 13+(i*2), 1);
				}

				// this is not really necessary in d3d9 (used a def instruction), but to be sure:
				f32 c95[] = {0.5f, 0.5f, 0.5f, 0.5f};
				services->setVertexShaderConstant(c95, 95, 1);
			}
		}
	};
#endif

//! Renderer for normal maps
#ifdef DELPHI_WRAP
	class CD3D9NormalMapRenderer :
		public CD3D9ShaderMaterialRenderer
#else
	class CD3D9NormalMapRenderer :
		public CD3D9ShaderMaterialRenderer, IShaderConstantSetCallBack
#endif

{

#ifdef DELPHI_WRAP

#endif

public:

	CD3D9NormalMapRenderer(
		IDirect3DDevice9* d3ddev, video::IVideoDriver* driver,
		s32& outMaterialTypeNr, IMaterialRenderer* baseMaterial);

	~CD3D9NormalMapRenderer();

	//! Called by the engine when the vertex and/or pixel shader constants for an
	//! material renderer should be set.
#ifdef DELPHI_WRAP
#else
	virtual void _STDCALL_ OnSetConstants(IMaterialRendererServices* services, s32 userData);
#endif

	virtual bool _STDCALL_ OnRender(IMaterialRendererServices* service, E_VERTEX_TYPE vtxtype);

	//! Returns the render capability of the material.
	virtual s32 _STDCALL_ getRenderCapability() const;

private:

};

} // end namespace video
} // end namespace irr

#endif
#endif
#endif

